﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

public partial class subjectUpLoad : System.Web.UI.Page
{
    //声明页面的公共变量
    public SqlCommand com;
    public int title_id;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化一个command对象
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["gConnectionString"]);
            conn.Open();
            com = new SqlCommand();
            com.Connection = conn;

        //初始化title_id共享变量
            if (Request.QueryString["title_id"] != null)
            {
                title_id = Convert.ToInt32(Request.QueryString["title_id"]);
                selectRecord();
                publish.Text = "修改";
            }
            else
            {
                publish.Text = "发表";
            }
    }
    protected void publish_Click(object sender, EventArgs e)
    {
        //发表一个课程
        if(publish.Text=="修改")
        {
            if (a.Text.Trim() != "")
            {
                updateRecord();
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "标题不能为空，请填写标题！"));
            }
        }
        else
        {
            if (a.Text.Trim() != "")
            {
                insertRecord();
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "标题不能为空，请填写标题！"));
            }
            
        }
    }

    //对课程的增、查、删该
    private void insertRecord()
    {
            string strTitle = a.Text.Trim().ToString();
            string strContent = richeditor.text.ToString();
            int intImportant = 0;
            if (important.Text.Trim() != "")
            {
               intImportant = Convert.ToInt16(important.Text.Trim());
            }

            string sql = "insert into bbs(f_title,f_content,f_date,f_username1,f_ip,f_imp,f_topic) values(@f_title,@f_content,@f_date,@f_userName1,@f_ip,@f_imp,@f_topic)";
            com.CommandType = CommandType.Text;
            com.CommandText = sql;
            com.Parameters.Clear();
            com.Parameters.AddWithValue("@f_title", strTitle);
            com.Parameters.AddWithValue("@f_content", strContent);
            com.Parameters.AddWithValue("@f_imp", intImportant);
            com.Parameters.AddWithValue("@f_date", DateTime.Now);
            com.Parameters.AddWithValue("@f_userName1",Session["userName1"].ToString());
            com.Parameters.AddWithValue("@f_ip", Request.UserHostAddress);
            com.Parameters.AddWithValue("@f_topic",Session["stopic"].ToString());
            int result = com.ExecuteNonQuery();

            if (result == 1)
            {
                Session["jumpToView"] = title_id;
                Response.Write("<script language='javascript'>alert('发表成功！');</script>");
                Response.Write("<script language='javascript'>window.parent.location.reload();</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('发表失败！');</script>");
            }     
    }
    private bool selectRecord()
    {
        string sql = "select * from bbs where title_id=@title_id";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@title_id", title_id);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            a.Text = dr["f_title"].ToString();
            richeditor.text = dr["f_content"].ToString();
            important.Text = dr["f_imp"].ToString();
            dr.Dispose();
            return true;
        }
        else
        {
            dr.Dispose();
            return false;
        }
    }
    private bool updateRecord()
    {
        string strTitle = a.Text.Trim().ToString();
        string strContent = richeditor.text.ToString();
        int intImportant = 0;
        if (important.Text.Trim() != "")
        {
            intImportant = Convert.ToInt16(important.Text.Trim());
        }

        string sql = "update [bbs] set f_title=@f_title,f_content=@f_content,f_imp=@f_imp,f_date=@f_date,f_userName1=@f_userName1,f_ip=@f_ip where title_id=@title_id";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@f_title", strTitle);
        com.Parameters.AddWithValue("@f_content", strContent);
        com.Parameters.AddWithValue("@f_imp", intImportant);
        com.Parameters.AddWithValue("@f_date", DateTime.Now);
        com.Parameters.AddWithValue("@f_userName1", Session["userName1"].ToString());
        com.Parameters.AddWithValue("@f_ip", Request.UserHostAddress);
        com.Parameters.AddWithValue("@title_id",title_id);
        int result = com.ExecuteNonQuery();
        if (result == 1)
        {
            Response.Write(string.Format("<script language='javascript'>alert('{0}__{1}___{2}');</script>", strTitle, intImportant, strContent));
            Response.Write("<script language='javascript'>alert('修改成功！');</script>");
            return true;
        }
        else
        {
            Response.Write("<script language='javascript'>alert('修改失败！');</script>");
            return false;
        }



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Button1.Text == "显示")
        {
            Button1.Text = "隐藏";
            richeditor.ToolbarDisplay = true;
        }
        else
        {
            Button1.Text = "显示";
            richeditor.ToolbarDisplay = false;
        }
    }

    protected int getInsertTitle_id(string title)
    {
        //获取刚插入bbs记录的title_id
        string sql = "select * from bbs where f_title=@f_title";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@f_title", title);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.Read())
        {
            
            int id =Convert.ToInt32(dr["title_id"].ToString());
            dr.Dispose();
            return id;
        }
        else
        {
            dr.Dispose();
            return 0;
        }
    }
}
